# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc


class SRMInquiryOfSupplier(Document):
	pass


# 生成报价单
@frappe.whitelist()
def make_srm_quotation(source_name, target_doc=None):
	if frappe.db.exists("SRM Quotation", {"from_inquiry_of_supplier": source_name}):
		frappe.msgprint("已经生成报价不允许重复")
	else:
		doc = get_mapped_doc(
			"SRM Inquiry Of Supplier",
			source_name,
			{
				"SRM Inquiry Of Supplier": {
					"doctype": "SRM Quotation",
					"field_map": {
						"supplier": "supplier_number",
						"supplier_name": "supplier_name",
						"name": "from_inquiry_of_supplier", 
						"from_inquiry": "from_inquiry",
						"need_mpq": "need_mpq",
						"need_moq": "need_moq",
						"need_lead_time": "need_lead_time"
					},
					"validation": {"status": "已报价"},
					# "condition": lambda doc: doc.status == '未关闭',
				},
				"SRM Inquiry Of Supplier Material Sub": {
					"doctype": "SRM Quotation Material Sub",
					"field_map": {
						"material_number": "material_number",
						"material_name": "material_name",
						"material_model": "material_model",
						"uom": "uom",
					},
					# "postprocess": update_item,
					# "condition": lambda doc: doc.status == '未关闭',
				},
			},
			target_doc,
		)
		# 这个set_value应该是在报价单保存后事件来完成
		# frappe.db.set_value("SRM Inquiry Of Supplier", source_name, "status", "已报价")
		# 下推生成的报价单不属于[自主报价]
		doc.independent_quotation = 0
		return doc